package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.EDeviceType;
import utils.db.impl.EditDevice;

/**
 * Servlet implementation class EditDeviceServlet
 */
@WebServlet("/EditDeviceServlet")
public class EditDeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int deviceId = Integer.parseInt(request.getParameter("deviceId"));
		int clientId = Integer.parseInt(request.getParameter("clientId"));
		int servicemanId = Integer.parseInt(request.getParameter("servicemanId"));
		String type = request.getParameter("type");
		String desc = request.getParameter("desc");
		HttpSession session;
		
		if(deviceId!=0 && clientId!=0 && servicemanId!=0 && !type.equals("") && !desc.equals("")){
			EDeviceType deviceType = EDeviceType.valueOf(type);
			EditDevice edit = new EditDevice();
			edit.complexUpdateDevice(deviceId, clientId, servicemanId, deviceType, desc);
			session=request.getSession(true);
			session.setAttribute("editDevice", "Zmiany zostały wprowadzone do bazy!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelDevice#pagsd");
		}else{
			session=request.getSession(true);
			session.setAttribute("editDevice", "Niektóre pola pozostały puste!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelDevice#pagsd");
		}
	}

}
